package com.nxs.hykd.ui.hengyang;

import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import android.os.Message;

import java.lang.ref.WeakReference;
import java.util.Map;

public class MainActivity extends FlutterActivity {
    public String tellerNo;
    public String tellerName;
    private MyHandler myHandler;
    private final static int CLOSE_APP = 1;
    final static class MyHandler extends Handler {
        private WeakReference<MainActivity> myActivity;
        MyHandler(MainActivity activity) {
            myActivity = new WeakReference<>(activity);
        }
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CLOSE_APP:
                    MainActivity activity = myActivity.get();
                    if (activity != null) {
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                    break;
            }
        }
    }
    @Override
    public void configureFlutterEngine(FlutterEngine flutterEngine){

        Log.e("tag", "sss");
        if (!getIntent().hasExtra("tellerNo") /*|| !Intent().hasExtra("tellerName")*/) {
            Toast.makeText(getApplicationContext(), "只允许从福祥到家登录，3秒后退出", Toast.LENGTH_SHORT).show();
            myHandler = new MyHandler(this);
            Message msg = new Message();
            msg.what = CLOSE_APP;
            myHandler.sendMessageDelayed(msg, 3000);
            return;
        } else {
            tellerNo = getIntent().getExtras() != null ? getIntent().getExtras().getString("tellerNo") : "";  //3100484
            tellerName = getIntent().getExtras() != null ? getIntent().getExtras().getString("tellerName") : ""; //朱华
            GeneratedPluginRegistrant.registerWith(flutterEngine);
            final String CHANNEL = "app.channel.shared.data";
            new MethodChannel(getFlutterEngine().getDartExecutor(), CHANNEL).setMethodCallHandler(
                    (call, result) -> {
                        System.out.println(call.method);
                        if (call.method.contentEquals("getIntentText")) {
                            result.success(tellerName+"/"+tellerNo);
                        }else if(call.method.contentEquals("exit")){
                            Toast.makeText(getApplicationContext(), "3秒后退出应用程序", Toast.LENGTH_SHORT).show();
                            myHandler = new MyHandler(this);
                            Message msg = new Message();
                            msg.what = CLOSE_APP;
                            myHandler.sendMessageDelayed(msg, 3000);
                            result.success("");
                        }else{
                            result.notImplemented();
                        }
                    });

        }

    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);





    }
}
